package com.chenglu.android.mine.me.mvp.presenter;


import android.accounts.NetworkErrorException;

import com.chenglu.android.bean.SmsCodeBean;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.ReturnStringBean;
import com.chenglu.android.mine.bean.UserBalanceBean;
import com.chenglu.android.mine.bean.UserInfoBean;
import com.chenglu.android.mine.me.mvp.contract.MineContract;
import com.chenglu.android.mine.me.mvp.model.MineModel;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *
 */
public class MinePresenter extends BasePresenter<MineContract.View> implements MineContract.MinePresenter {

     private  MineContract.MineModel model;

    public MinePresenter(){
        model = new MineModel();
    }



    @Override
    public void getUserInfo(String token) {
        if(!isViewAttached()){
            return;
        }
        model.getUserInfo(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<UserInfoBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull UserInfoBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getUserInfo(bean);
                        }else {
                            mView.getUserInfo(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        if(e instanceof ConnectException || e instanceof NetworkErrorException ||e instanceof UnknownHostException){
                            ToastUtils.show("请检查您的网络状态！");
                        }else if(e instanceof TimeoutException || e instanceof SocketTimeoutException){
                            ToastUtils.show("请求超时！");
                        }
                        else  {
                            if(e.getMessage().contains("401")){
                                //401 登录异常
                                mView.onError(e.getMessage());
                            }else {
                                //401 登录异常
                                ToastUtils.show("服务器连接失败");
                            }

                        }
                        //记得上线删除
//                        ToastUtils.show(e.getMessage());
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getUserBalance(String token) {
        if(!isViewAttached()){
            return;
        }
        model.getUserBalance(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<UserBalanceBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull UserBalanceBean bean) {
                        LogUtils.e("qqqqqqq:"+"onNext");

//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getUserBalance(bean);
                        }else {
                            mView.getUserBalance(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        if (e instanceof ConnectException || e instanceof NetworkErrorException || e instanceof UnknownHostException) {
                            ToastUtils.show("网络不可用，请检查您的网络状态！");

                        } else if (e instanceof TimeoutException || e instanceof SocketTimeoutException) {
                            ToastUtils.show("请求超时！");
                        } else {
                            //401 登录异常
                            if(e.getMessage().contains("401")){
//                                ToastUtils.show("登录失效，请重新登录");
                                mView.onUserBalanceError(e.getMessage());
                            }else {
//                                ToastUtils.show("服务器连接失败");
                            }


                        }
                        LogUtils.e("qqqqqqq:" + "onError:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });
    }

    @Override
    public void getExitLogin(String token) {
        if(!isViewAttached()){
            return;
        }
        model.getExitLogin(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<SmsCodeBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull SmsCodeBean bean) {
//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getExitLogin(bean);
                        }else {
                            mView.getExitLogin(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        if (e instanceof ConnectException || e instanceof NetworkErrorException || e instanceof UnknownHostException) {
                            ToastUtils.show("请检查您的网络状态！");

                        } else if (e instanceof TimeoutException || e instanceof SocketTimeoutException) {
                            ToastUtils.show("请求超时！");
                        } else {
                            //401 登录异常
                            if(e.getMessage().contains("401")){
                                ToastUtils.show("登录失效，请重新登录");
//                                mView.onError(e.getMessage());
                            }else {
                                ToastUtils.show("服务器连接失败");
                            }


                        }
                        LogUtils.e("qqqqqqq:" + "onError:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();


                    }
                });
    }

    @Override
    public void getPlatformPics() {
        if(!isViewAttached()){
            return;
        }
        model.getPlatformPics()
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<ReturnStringBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();

                    }

                    @Override
                    public void onNext(@NonNull ReturnStringBean bean) {


//                        mView.getUserInfoSuccess(bean);
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getPlatformPics(bean);
                        }else {
                            mView.getPlatformPics(bean);
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        if (e instanceof ConnectException || e instanceof NetworkErrorException || e instanceof UnknownHostException) {
                            ToastUtils.show("请检查您的网络状态！");

                        } else if (e instanceof TimeoutException || e instanceof SocketTimeoutException) {
                            ToastUtils.show("请求超时！");
                        } else {
                            //401 登录异常
                            if(e.getMessage().contains("401")){
                                ToastUtils.show("登录失效，请重新登录");
//                                mView.onError(e.getMessage());
                            }else {
                                ToastUtils.show("服务器连接失败");
                            }


                        }
                        LogUtils.e("qqqqqqq:" + "onError:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();

                    }
                });
    }
}
